import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getAllBookApi } from "../../request/bookInfo/getAllBook";

interface oneBook {
    id:number,
    book_name:string,
    book_public:string,
    book_price:number,
    book_profile:string,
    book_count:number,
    img_url:string,
    book_writer:string

}

interface allBook{
    bookInfo:oneBook[]
}

// 初始化数据
const initialState:allBook = {
    bookInfo:[]
}

// 创建thunk，用于发送异步请求获取数据

// 获取所有书籍的所有信息
export const getAllBooksAsync:any = createAsyncThunk(
    "books/getAllBooksAsync",
    async ()=>{
        return await getAllBookApi()
    }
)



const booksSlice = createSlice({
    name:"books",
    initialState,
    reducers:{
        
    },
    extraReducers(build){
        build
        .addCase(getAllBooksAsync.fulfilled, (state, action) => {
            state.bookInfo = action.payload.allBook||null
        })
    }
})

const reducer = booksSlice.reducer
export default reducer
